import sys
parent_dir = "../pygl"
sys.path.append(parent_dir)
import ctypes
import OpenGL.GL as gl
import glm
import imgui
import glfw
from glfw import _GLFWwindow as GLFWwindow
import numpy as np
import pygl
from common import AppWithTrackball, Sphere, AxesHelper

class SphereApp(AppWithTrackball):
    # 初始化着色器，vao vbo等
    def setupVertex(self) -> None:
        gl.glEnable(gl.GL_DEPTH_TEST)
        self.program = pygl.ProgramVF("intermediate/shaders/geometry.vs", 
                                      "intermediate/shaders/geometry.fs")
        self.background_color = glm.vec4(0.3, 0.3, 0.3, 1.0)
        self.sphere = Sphere()
        self.world_axes = AxesHelper(1.5, 5.0)
        self.show_world_axes = True
        self.imgui_window_width = 250
        self.imgui_window_height = 200
        self.sphere_color = glm.vec3(1.0, 0.0, 0.0)
        self.line_color = glm.vec3(1.0, 1.0, 1.0)
        self.wireframe = 0
        self.sphere_texture = pygl.ImageTexture2D("textures/2k_earth_daymap.jpg")

    # 配置imgui界面元素
    def showImGui(self)->None:
        imgui.begin("设置")
        if imgui.button("重置"):
            self.camera.reset(glm.vec3(0.0, 0.0, 3.0), glm.vec3(0.0, 0.0, 0.0), 
                    glm.vec3(0.0, 1.0, 0.0))
        if imgui.radio_button("线框模式", self.wireframe == 0):
            self.wireframe = 0
        if imgui.radio_button("实体模式", self.wireframe == 1):
            self.wireframe = 1
        if imgui.radio_button("纹理模式", self.wireframe == 2):
            self.wireframe = 2
        if self.wireframe == 0:
            _, color = imgui.color_edit3("线框颜色", self.line_color.x, 
                        self.line_color.y, self.line_color.z)
            self.line_color = glm.vec3(color[0], color[1], color[2])
        elif self.wireframe == 1:
            _, color = imgui.color_edit3("球体颜色", self.sphere_color.x, 
                        self.sphere_color.y, self.sphere_color.z)
            self.sphere_color = glm.vec3(color[0], color[1], color[2])
        _, self.show_world_axes = imgui.checkbox("显示三维世界坐标轴", 
                                                self.show_world_axes)
        imgui.end()       

    # 渲染图形
    def render(self) -> None:
        gl.glClearBufferfv(gl.GL_COLOR, 0, self.background_color.to_tuple())
        gl.glClearBufferfv(gl.GL_DEPTH, 0, 1.0)
        self.view = self.camera.getViewMatrix()
        self.projection = self.camera.getProjectionMatrix()
        self.program.use()
        self.sphere.updateModelMatrix()
        self.program.setUniformMatrix4fv(0, self.sphere.model_matrix)
        self.program.setUniformMatrix4fv(1, self.view)
        self.program.setUniformMatrix4fv(2, self.projection)
        if self.wireframe == 0:
            self.program.setUniform1i(3, 0)
            self.program.setUniform3fv(5, self.line_color)
            gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)
            gl.glLineWidth(1.0)
        elif self.wireframe == 1:
            self.program.setUniform1i(3, 1)
            self.program.setUniform3fv(4, self.sphere_color)
            gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
        elif self.wireframe == 2:
            self.program.setUniform1i(3, 2)
            gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
            self.sphere_texture.bind(0)
        self.sphere.render()
        if self.show_world_axes:
            self.world_axes.updateModelMatrix
            self.world_axes.render(self.view, self.projection)

    # 释放资源
    def cleanup(self) -> None:
        self.sphere.delete()
        self.world_axes.delete()
        self.program.delete()
        self.sphere_texture.delete()

    def mouse_move(self, window:GLFWwindow, x:float, y:float)->None:
        if imgui.is_any_item_active():
            return
        super().mouse_move(window, x, y)

if __name__ == "__main__":
    app = SphereApp(100, 100, 1000, 600, "你好，地球App！")
    app.run()


